package com.gemfire.onlineretailer.domain;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Date;

import com.gemstone.gemfire.DataSerializable;
import com.gemstone.gemfire.DataSerializer;

public class Book extends Item implements DataSerializable {

	  public static final long BOOK_1 = 10000001;
	  public static final long BOOK_2 = 10000002;

	  public static final long BOOK_3 = 10000003;
	  public static final long BOOK_4 = 10000004;
	  public static final long BOOK_5 = 10000005;
	  
	  public static final String BOOK_1_CODE = "10000001";
	  public static final String BOOK_2_CODE = "10000002";

	  public static final String BOOK_3_CODE = "10000003";
	  public static final String BOOK_4_CODE = "10000004";
	  public static final String BOOK_5_CODE = "10000005";
	  
	  public static final String BOOK_1_AUTHOR = "Peter Norton";
	  public static final String BOOK_2_AUTHOR = "Hertert Schieldt";
	  public static final String BOOK_3_AUTHOR = "Damodar Chetty";
	  public static final String BOOK_4_AUTHOR = "Sydney Sheldon";
	  public static final String BOOK_5_AUTHOR = "Agatha Christie";
	  
	  public static final String BOOK_1_TITLE = "MASTERING DOS 6";
	  public static final String BOOK_2_TITLE = "Java Complete Reference";
	  public static final String BOOK_3_TITLE = "Tomcat 6 Develoepr Guide";
	  public static final String BOOK_4_TITLE = "The Other side of Midnight";
	  public static final String BOOK_5_TITLE = "Murder She Wrote";

	  
	private long bookSerialNumber;
	private String title;
	private String bookISBN;
	private String author;
	private Date publishDate;
	private int numPages;
	private String publisher;
	
	public Book(){
		super();
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public long getBookSerialNumber() {
		return bookSerialNumber;
	}
	public void setBookSerialNumber(long bookSerialNumber) {
		this.bookSerialNumber = bookSerialNumber;
	}
	public String getBookISBN() {
		return bookISBN;
	}
	public void setBookISBN(String bookISBN) {
		this.bookISBN = bookISBN;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public Date getPublishDate() {
		return publishDate;
	}
	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}
	public int getNumPages() {
		return numPages;
	}
	public void setNumPages(int numPages) {
		this.numPages = numPages;
	}
	public String getPublisher() {
		return publisher;
	}
	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	@Override
	public void fromData(DataInput in) throws IOException,
			ClassNotFoundException {
		this.bookSerialNumber = in.readLong();
		this.title = in.readUTF();
		this.bookISBN = in.readUTF();
		this.author = in.readUTF();
		this.publishDate = DataSerializer.readDate(in);
		this.numPages = in.readInt();
		this.publisher = in.readUTF();
		
	}

	@Override
	public void toData(DataOutput out) throws IOException {
		
		out.writeLong(this.bookSerialNumber);
		out.writeUTF(this.title);
		out.writeUTF(this.bookISBN);
		out.writeUTF(this.author);
		DataSerializer.writeDate(this.publishDate, out);
		out.writeInt(this.numPages);
		out.writeUTF(this.publisher);
		
	}
	
	
public static Book createInstance(long bookId){
		
		/*
		 * Use itemId to read from DB or from other source, Hardcode for now
		 */
		
		Book book = new Book();
		
		if (bookId == BOOK_1){
			book.setItemId(bookId);
			book.setAuthor(BOOK_1_AUTHOR);
			book.setBookISBN(BOOK_1_CODE);
			book.setTitle(BOOK_1_TITLE);
			book.setReorderLevel(30);
		} 
		else if (bookId == BOOK_2){
			book.setItemId(bookId);
			book.setAuthor(BOOK_2_AUTHOR);
			book.setBookISBN(BOOK_2_CODE);
			book.setTitle(BOOK_2_TITLE);
			book.setReorderLevel(40);
		} 
		else if (bookId == BOOK_3){
			book.setItemId(bookId);
			book.setAuthor(BOOK_3_AUTHOR);
			book.setBookISBN(BOOK_3_CODE);
			book.setTitle(BOOK_3_TITLE);
			book.setReorderLevel(70);
		} 
		else if (bookId == BOOK_4){
			book.setItemId(bookId);
			book.setAuthor(BOOK_4_AUTHOR);
			book.setBookISBN(BOOK_4_CODE);
			book.setTitle(BOOK_4_TITLE);
			book.setReorderLevel(50);
		}
		else {
			book.setItemId(bookId);
			book.setAuthor(BOOK_5_AUTHOR);
			book.setBookISBN(BOOK_5_CODE);
			book.setTitle(BOOK_5_TITLE);
			book.setReorderLevel(60);
			} 
		
		return book;
		
	}
	
}
